package com.wangzh.nio;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class FileCopyDemo {

    public static void main(String[] args) throws IOException {
        /**
         * D:\learn\aa.mp4
         */


        test2();
    }

    private static void test2() throws IOException {
        //        创建对象


        try (FileInputStream fis = new FileInputStream("D:\\learn\\aa.mp4");
             FileOutputStream fos = new FileOutputStream("D:\\learn\\aacopy.mp4"))
        {

//        拷贝
            int len;
            byte[] bytes = new byte[1024*1024];
            while ((len =fis.read(bytes)) != -1){
                fos.write(bytes,0,len);
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }


    }
    private static void test1() throws IOException {
        //        创建对象
        FileInputStream fis = null;
        FileOutputStream fos = null;
        try {
            fis = new FileInputStream("D:\\learn\\aa.mp4");
            fos = new FileOutputStream("D:\\learn\\aacopy.mp4");

//        拷贝
            int len;
            byte[] bytes = new byte[1024*1024];
            while ((len =fis.read(bytes)) != -1){
                fos.write(bytes,0,len);
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            if (fis!=null) {
                fis.close();
            }

            if (fos!=null) {
                fos.close();
            }

        }


    }
    private static void test() throws IOException {
        //        创建对象
        FileInputStream fis = new FileInputStream("D:\\learn\\aa.mp4");
        FileOutputStream fos = new FileOutputStream("D:\\learn\\aacopy.mp4");

//        拷贝
        int len;
        byte[] bytes = new byte[1024*1024];
        while ((len =fis.read(bytes)) != -1){
            fos.write(bytes,0,len);
        }

//        关闭
        fis.close();
        fos.close();
    }
}
